#ifndef DIRECTORIES_AND_FILES_H_
#define DIRECTORIES_AND_FILES_H_

#ifdef _MSC_VER
#include<io.h>
#include<stdio.h>
#else
#include<stdio.h>
#include<dirent.h>
#endif


#ifdef __cplusplus
extern "C"{
#endif
static char** search_dir_files(char* path,int*count);

#ifdef __cplusplus
}
#endif

#ifdef _MSC_VER
//_A_ARCH(存档) _A_NORMAL(正常) _A_HIDDEN(隐藏)
// _A_RDONLY(只读) _A_SUBDIR(文件夹) _A_SYSTEM(系统)
// _A_HIDDEN | _A_RDONLY (隐藏+只读)
static char** search_dir_files(char* path,int* count)
{
    char** re=NULL;
    struct _finddata_t data;
    long hnd=_findfirst(path,&data);
    if (hnd<0)
    {
        printf("cuowu \n");
        return NULL;
    }
    int nRet=(hnd<0)?-1:1;
    *count=0;
    while(nRet>=0)
    {
        if(data.attrib==_A_NORMAL)
        {
            *count=*count+1;
        }
        nRet= _findnext(hnd,&data);
    }
    _findclose(hnd);

    hnd=_findfirst(path,&data);
    int i=0;
    re=(char**)malloc(sizeof(char*)*(*count));
    while(nRet>=0)
    {
        if(data.attrib==_A_NORMAL)
        {
            re[i]=(char*)malloc(sizeof(char)*(strlen(data.name)+1));
            strcpy(re[i],data.name);
            i++;
        }
        nRet= _findnext(hnd,&data);
    }
   _findclose(hnd);
   return re;
}
#else
static char** search_dir_files(char*path,int * count)
{
    printf("mingw\n");
    
    char** re=NULL;
    DIR* dp=opendir(path);
    struct dirent*dirp=NULL;
    *count=0;
    if(dp==NULL)
    {
        return NULL;
    }
    long int loc= telldir(dp);
    
    while((dirp=readdir(dp))!=NULL)
    {
        *count=*count+1;
    }
    re=(char**)malloc(sizeof(char*)*(*count));
    seekdir(dp,loc);
    int i=0;
    while((dirp=readdir(dp))!=NULL)
    {
        re[i]=(char*)malloc(sizeof(char)*(strlen(dirp->d_name)+1));
        strcpy(re[i],dirp->d_name);
        i++;
    }
    closedir(dp); 

    return re;
}


#endif 


#endif